package org.jsola.hr.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.jsola.approval.dto.IssuedApproFlowAddDTO;
import org.jsola.contract.dto.IssuedCategoryAddDTO;
import org.jsola.contract.dto.IssuedSealAddDTO;
import org.jsola.core.entity.BaseDO;
import org.jsola.hr.dto.*;
import org.jsola.hr.entity.CustomTemplateDO;
import org.jsola.hr.provider.*;
import org.jsola.hr.service.*;
import org.jsola.salary.dto.IssuedSalaryTemplateAddDTO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 用工单位
 *
 * @author zhr
 */
@Slf4j
@Service("hrCompanySueServiceImpl")
public class CompanySueServiceImpl implements ICompanySueService {


    @Autowired
    @Lazy
    private ICompanyService companyService;
    @Autowired
    @Lazy
    private ISalaryCalculateRuleGroupService salaryCalculateRuleGroupService;
    @Autowired
    @Lazy
    private IContractProviderService contractProviderService;
    @Autowired
    @Lazy
    private ISalaryProviderService salaryProviderService;
    @Autowired
    @Lazy
    private IApproProviderService approProviderService;
    @Autowired
    @Lazy
    private ICustomTemplateService customTemplateService;
    @Autowired
    private ISocProgService socProgService;


    @Async
    @Override
    public void sueSalaryCalculateRuleGroup(AddAdvancedConfigurationDTO addAdvancedConfigurationDTO
            , List<String> salaryCalculateRuleGroupIdList, TokenUser tokenUser) {
        log.info("下发工价/劳务费规则线程：{}开始执行", Thread.currentThread().getName());
        long currentTimeMillis = System.currentTimeMillis();
        try {
            IssueSalaryCalculateRuleDTO issueSalaryCalculateRuleDTO = new IssueSalaryCalculateRuleDTO();
            issueSalaryCalculateRuleDTO.setCompanyIdList(Collections.singletonList(addAdvancedConfigurationDTO.getCompanyId()));
            issueSalaryCalculateRuleDTO.setSalaryCalculateRuleGroupIdList(salaryCalculateRuleGroupIdList);
            salaryCalculateRuleGroupService.issueGroup(issueSalaryCalculateRuleDTO, tokenUser);
            addAdvancedConfigurationDTO.setSalaryCalculate(System.currentTimeMillis() - currentTimeMillis);
        } catch (Exception e) {
            log.error("下发工价/劳务费规则失败：", e);
        } finally {
            addAdvancedConfigurationDTO.getCountDownLatch().countDown();
        }

    }


    @Async
    @Override
    public void sueCategory(AddAdvancedConfigurationDTO addAdvancedConfigurationDTO
            , List<IssuedCategoryAddDTO.Category> categoryList, Boolean isSeal, TokenUser tokenUser) {
        log.info("下发合同模版列表线程：{}开始执行", Thread.currentThread().getName());
        long currentTimeMillis = System.currentTimeMillis();
        try {
            IssuedCategoryAddDTO issuedCategoryAddDTO = new IssuedCategoryAddDTO();
            issuedCategoryAddDTO.setCategoryList(categoryList);
            IssuedCategoryAddDTO.Subject subject = new IssuedCategoryAddDTO.Subject();
            subject.setSubjectId(addAdvancedConfigurationDTO.getCompanyId());
            subject.setSubjectName(addAdvancedConfigurationDTO.getCompanyName());
            issuedCategoryAddDTO.setSubjectList(Collections.singletonList(subject));
            issuedCategoryAddDTO.setIsSeal(isSeal);
            contractProviderService.saveIssuedCategory(issuedCategoryAddDTO, tokenUser);
            addAdvancedConfigurationDTO.setCategory(System.currentTimeMillis() - currentTimeMillis);
        } catch (Exception e) {
            log.error("下发合同模版列表失败：", e);
        } finally {
            addAdvancedConfigurationDTO.getCountDownLatch().countDown();
        }
    }


    @Async
    @Override
    public void sueSalaryTemplate(AddAdvancedConfigurationDTO addAdvancedConfigurationDTO
            , List<Long> salaryTemplateIds, TokenUser tokenUser) {
        log.info("下发月工资模板线程：{}开始执行", Thread.currentThread().getName());
        long currentTimeMillis = System.currentTimeMillis();
        try {
            IssuedSalaryTemplateAddDTO issuedSalaryTemplateAddDTO = new IssuedSalaryTemplateAddDTO();
            issuedSalaryTemplateAddDTO.setSalaryTemplateIds(salaryTemplateIds);
            issuedSalaryTemplateAddDTO.setCompanyId(Collections.singletonList(addAdvancedConfigurationDTO.getCompanyId()));
            salaryProviderService.saveIssuedSeal(issuedSalaryTemplateAddDTO, tokenUser);
            addAdvancedConfigurationDTO.setSalaryTemplate(System.currentTimeMillis() - currentTimeMillis);
        } catch (Exception e) {
            log.error("下发月工资模板失败：", e);
        } finally {
            addAdvancedConfigurationDTO.getCountDownLatch().countDown();
        }
    }


    @Async
    @Override
    public void suedApproFlow(AddAdvancedConfigurationDTO addAdvancedConfigurationDTO
            , List<IssuedApproFlowAddDTO.ApproFlowInfor> approFlowInforList, TokenUser tokenUser) {
        log.info("下发审批线程：{}开始执行", Thread.currentThread().getName());
        long currentTimeMillis = System.currentTimeMillis();
        try {
            IssuedApproFlowAddDTO issuedApproFlowAddDTO = new IssuedApproFlowAddDTO();
            issuedApproFlowAddDTO.setCompanyIds(Collections.singletonList(addAdvancedConfigurationDTO.getCompanyId()));
            issuedApproFlowAddDTO.setApproFlowInforList(approFlowInforList);
            approProviderService.saveIssuedApproFlow(issuedApproFlowAddDTO, tokenUser);
            addAdvancedConfigurationDTO.setApproFlow(System.currentTimeMillis() - currentTimeMillis);
        } catch (Exception e) {
            log.error("下发审批失败：", e);
        } finally {
            addAdvancedConfigurationDTO.getCountDownLatch().countDown();
        }
    }


    @Async
    @Override
    public void sueTemplate(AddAdvancedConfigurationDTO addAdvancedConfigurationDTO
            , List<String> templateIds, TokenUser tokenUser) {
        log.info("下发入职模板线程：{}开始执行", Thread.currentThread().getName());
        long currentTimeMillis = System.currentTimeMillis();
        try {
            CustomDTO customDTO = new CustomDTO();
            customDTO.setCompanyIdList(Collections.singletonList(addAdvancedConfigurationDTO.getCompanyId()));
            customDTO.setTemplateIdList(templateIds);
            customTemplateService.issueTemplate2(customDTO, tokenUser);
            addAdvancedConfigurationDTO.setTemplate(System.currentTimeMillis() - currentTimeMillis);
        } catch (Exception e) {
            log.error("下发入职模板失败：", e);
        } finally {
            addAdvancedConfigurationDTO.getCountDownLatch().countDown();
        }
    }


    @Async
    @Override
    public void suedSocProg(AddAdvancedConfigurationDTO addAdvancedConfigurationDTO
            , List<String> socProgsIds, TokenUser tokenUser) {
        log.info("下发参保方案线程：{}开始执行", Thread.currentThread().getName());
        long currentTimeMillis = System.currentTimeMillis();
        try {
            IssuedSocProgAddDTO issuedSocProgAddDTO = new IssuedSocProgAddDTO();
            issuedSocProgAddDTO.setSocProgIds(socProgsIds);
            issuedSocProgAddDTO.setCompanyIds(Collections.singletonList(addAdvancedConfigurationDTO.getCompanyId()));
            socProgService.saveIssuedSocProg(issuedSocProgAddDTO, tokenUser);
            addAdvancedConfigurationDTO.setSocProg(System.currentTimeMillis() - currentTimeMillis);
        } catch (Exception e) {
            log.error("下发参保方案失败：", e);
        } finally {
            addAdvancedConfigurationDTO.getCountDownLatch().countDown();
        }

    }


    @Async
    @Override
    public void suedSeal(AddAdvancedConfigurationDTO addAdvancedConfigurationDTO, List<Long> sealsIds, TokenUser tokenUser) {
        log.info("下发印章线程：{}开始执行", Thread.currentThread().getName());
        long currentTimeMillis = System.currentTimeMillis();
        try {
            IssuedSealAddDTO issuedSealAddDTO = new IssuedSealAddDTO();
            issuedSealAddDTO.setCategoryIds(sealsIds);
            issuedSealAddDTO.setSubjectIds(Collections.singletonList(addAdvancedConfigurationDTO.getCompanyId()));
            contractProviderService.saveIssuedSeal(issuedSealAddDTO, tokenUser);
            addAdvancedConfigurationDTO.setSeal(System.currentTimeMillis() - currentTimeMillis);
        } catch (Exception e) {
            log.error("下发印章失败：", e);
        } finally {
            addAdvancedConfigurationDTO.getCountDownLatch().countDown();
        }
    }

}




